package com.lkm.app_ui.m.Imple.secondScreenManager;

import com.lkm.app.event.OnCreateActivityEvent;
import com.lkm.app_ui.m.fun.OnSaleManifestSyncEvent;
import com.lkm.app_ui.m.display_second.DisplaySecondModule;
import com.lkm.app_ui.m.display_second.OnDisplaySecondShowChangeEvent;
import com.lkm.app_ui.m.fun.OnPayEvent;
import com.lkm.app_ui.m.secondScreenManager.SecondScreenManagerModule;
import com.lkm.app_ui.ui.MainActivity;
import com.lkm.app_ui.ui.second.UIManifest;
import com.lkm.comlib.ui.BaseFragmentActivity;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.Observer;
import com.lkm.javatool.CollectionHelp;

import androidx.fragment.app.Fragment;




public class SecondScreenManagerModuleImple implements SecondScreenManagerModule {


    public SecondScreenManagerModuleImple(DisplaySecondModule mDisplaySecondModule) {
        this.mDisplaySecondModule=mDisplaySecondModule;
        init();
    }

    public DisplaySecondModule mDisplaySecondModule;

    



    public void init(){
        LinkBus.registerReceiver(onSaleManifestSyncEventObserver);
        LinkBus.registerReceiver(onDisplaySecondShowChangeEventObserver);


        if(BaseFragmentActivity.getLastActivity()!=null&&BaseFragmentActivity.getLastActivity().getClass()== MainActivity.class){
            
            holdIdleUIIfNeed();
        }else{
            LinkBus.registerReceiver(onCreateActivityEventObserver);
        }

        LinkBus.registerReceiver(new Observer<OnPayEvent>() {
            @Override
            public Class<OnPayEvent> getBusEventClass() {
                return OnPayEvent.class;
            }
            @Override
            public void onReceiver(OnPayEvent data, Runnable nextRun) {
                try {
                    
                    mDisplaySecondModule.dismissOther(UIManifest.getSaleingFragment());
                } finally {
                    nextRun.run();
                }

            }
        });
    }

    @Override
    public void showPayFragment() {

    }

    @Override
    public void showPayOutlineFragment() {

    }

    
    private void holdIdleUIIfNeed(){
        
        if(mDisplaySecondModule.getLast()==null&&BaseFragmentActivity.getLastActivity()!=null&&BaseFragmentActivity.getLastActivity().getClass()== MainActivity.class){

            if(mDisplaySecondModule.getBase()!=null&&mDisplaySecondModule.getBase().getClass()==UIManifest.getIdleFragment()){

            }else {
                
                mDisplaySecondModule.showSingleInstanceInBase(BaseFragmentActivity.getLastActivity(), UIManifest.getIdleFragmentObject());
            }
        }
    }

    
    private Observer<OnCreateActivityEvent> onCreateActivityEventObserver=new Observer<OnCreateActivityEvent>() {
        @Override
        public Class<OnCreateActivityEvent> getBusEventClass() {
            return OnCreateActivityEvent.class;
        }

        @Override
        public void onReceiver(OnCreateActivityEvent data, Runnable nextRun) {

            try{

                if(data.extActivity.getClass()==MainActivity.class) {


                    holdIdleUIIfNeed();
//                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {





                }

            }finally {
                nextRun.run();
            }
        }
    };

    
    private Observer<OnDisplaySecondShowChangeEvent> onDisplaySecondShowChangeEventObserver=new Observer<OnDisplaySecondShowChangeEvent>() {
        @Override
        public Class<OnDisplaySecondShowChangeEvent> getBusEventClass() {
            return OnDisplaySecondShowChangeEvent.class;
        }

        @Override
        public void onReceiver(OnDisplaySecondShowChangeEvent data, Runnable nextRun) {
            try{

                holdIdleUIIfNeed();

                Fragment lastFragment = mDisplaySecondModule.getLast();

                if(lastFragment!=null){
                    
                    if(lastFragment.getClass()==UIManifest.getSaleingFragment()){
                        mDisplaySecondModule.dismissExceptLast();
                    }

                }


            }finally {
                nextRun.run();
            }

        }
    };


    
    private Observer<OnSaleManifestSyncEvent> onSaleManifestSyncEventObserver = new Observer<OnSaleManifestSyncEvent>() {
        @Override
        public Class getBusEventClass() {
            return OnSaleManifestSyncEvent.class;
        }

        @Override
        public void onReceiver(OnSaleManifestSyncEvent data, Runnable nextRun) {

            try{

                
                if(!CollectionHelp.isEmpty(data.extSaleManifestEntity.getSouceList())){
                    
                    Fragment lastFragment=mDisplaySecondModule.getLast();
                    if(lastFragment==null||lastFragment.getClass()!= UIManifest.getSaleingFragment()){
                        
                        mDisplaySecondModule.dismissAll(UIManifest.getSaleingFragment());
                        mDisplaySecondModule.show(UIManifest.getSaleingFragmentObject());
                    }
                }else{
                    
                    mDisplaySecondModule.dismissAll(UIManifest.getSaleingFragment());
                }



            }finally {
                nextRun.run();
            }
        }
    };


      

}
